class FriendsController < ApplicationController

  def index
    @requests = Request.where("sender_id = ? or (receiver_id = ? and status = true)", current_user.id, current_user.id)
    ids = []
    @friends = User.find_all_by_id @requests.map{ |row| row.sender_id == current_user.id ? row.receiver_id : row.sender_id}
  end

  def add
    requests = Request.where("(sender_id = ? and receiver_id = ?) or (sender_id = ? and receiver_id = ?)", current_user.id, params[:friend_id], params[:friend_id], current_user.id)
    unless requests.empty?
      request = requests.last
      if (current_user.id == request.receiver_id) and !request.status
        request.status = true
        request.save
      end
    else
      Request.create(:sender_id => current_user.id, :receiver_id => params[:friend_id])
    end
    redirect_to friends_path
  end

  def online
    index

  end

end
